/*
 * @Author: duanguang
 * @Date: 2019-11-07 09:31:57
 * @LastEditTime: 2022-03-03 15:33:21
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-server/app/model/user.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
import { UserRoleEnum } from '@/constants/enum.types';
import { Application } from 'egg';
import { Model, Document, Schema } from './index.d';
import {UserRoleType} from '@/interface/user.interface'

export enum EUserPwdStrength {
  /** 强密码 */
  STRONG = 'strong',
  /** 良好密码 */
  ENOUGH = 'enough',
  /** 弱密码 */
  WEEK = 'week',
  /** 不安全密码 */
  UNSAFE = 'unsafe',
}
export interface UserInfoEntity {
  id: string;
  uuid: string;
  name: string;
  email: string;
  phone: string;
  role: UserRoleType;
  roleNo: Number;
}

export interface IUser {
  uuid: string;
  name: string;
  role: UserRoleType;
  roleNo: Number;
  password: string;
  pwdStrength: EUserPwdStrength;
  phone?: string;
  email?: string;
  gitlab_open_id?: string;
  nickName?: string;
  avatar?: string;
  jToken?: string;
  createTime: Date;
  updateTime: Date;
  lastLoginTime: Date;
}

export interface IApplicationUserModel extends Document, IUser {}
export default function UserModel(app: Application): Model<IApplicationUserModel> {
  const Mongoose = app.mongoose;
  const Schema = Mongoose.Schema;
  const UserSchema: Schema = new Schema({
    /** 唯一id */
    uuid: { type: String },
    /** 用户名 */
    name: { type: String },
    /** 性别 */
    sex: { type: String },
    /** 密码 */
    password: { type: String },
    /** 密码强度 */
    pwdStrength: { type: String },
    /** 角色 */
    role: { type: String, default: UserRoleEnum.common[UserRoleEnum.common] },
    /** 角色数字，根据角色的级别递减，数字越小，级别越高 */
    roleNo: { type: Number, default: UserRoleEnum.common },
    /** 电话 */
    phone: { type: String },
    /** 邮箱 */
    email: { type: String },
    /** 安全邮箱 */
    pwdEmail: { type: String },
    /** gitlab openid */
    gitlab_open_id: { type: String },
    /** 微信昵称 */
    nickName: { type: String },
    /** 微信头像 */
    avatar: { type: String },
    /** jenkins token 用户登录凭证 */
    jToken: { type: String },
    /** 用户所属公司 */
    belongCenter: { type: String },
    /** 用户所属部门 */
    belongDepartment: { type: String },
    /** 用户最后登录的时间 */
    lastLoginTime: { type: Date },
  }, {
    timestamps: { updatedAt: 'updateTime', createdAt: 'createTime' },
  });
  return Mongoose.model<IApplicationUserModel>('HLSUser', UserSchema);
}
